<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\GoodsRequest;
use App\models\Bale;
use App\models\Category;
use App\models\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $goods = Goods::paginate(6);
        return view('admin.goods_index',compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categorys = Category::where('parent_id',0)->get();
       return view('admin.goods_create',compact('categorys'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(GoodsRequest $request)
    {
//        dd($request->all());
        $goods = Goods::create($request->all());
//        获取request中的product货品字段
        $bales = json_decode($request->product,true);
        foreach ($bales as $bale)
        {
            Bale::create(
                [
                  'natures' => $bale['natures'],
                    'stock' => $bale['stock'],
                    'attach' => $bale['attach'],
                    'goods_id' => $goods['id'],
                ]
            );
        }
        return redirect(route('admin.goods.index'))->with('success','添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function show(Goods $good)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function edit(Goods $good)
    {
//        获取所有的顶级分类
        $categorys = Category::where('parent_id',0)->get();
//        获取当前分类的二级子分类
        $twoCategory = Category::where('parent_id',$good['category_id'][0])->get();
//        获取三级子分类
        $threeCategory = Category::where('parent_id',$good['category_id'][1])->get();
//        获取当前商品的货品数据
        $bales = $good->bale;
        return view('admin.goods_edit',compact('good','categorys','twoCategory','threeCategory','bales'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function update(GoodsRequest $request, Goods $good)
    {
       $good->update($request->all());
       $bales = json_decode($request->product,true);
//       先将当前商品对应的货品全部删除
        Bale::where('goods_id',$good['id'])->delete();
        foreach ($bales as $bale)
        {
            Bale::create([
                    'natures' => $bale['natures'],
                    'stock' => $bale['stock'],
                    'attach' => $bale['attach'],
                    'goods_id' => $good['id'],
            ]);
        }
        return redirect(route('admin.goods.index'))->with('success','商品信息修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Goods  $goods
     * @return \Illuminate\Http\Response
     */
    public function destroy(Goods $good)
    {
        $good->delete();
        return redirect(route('admin.goods.index'))->with('success','商品删除成功');
    }
}
